import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/major_hazard/hazard_instrument_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/major_hazard_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/monitor/monitor_type.dart';

class HazardMonitorController extends GetxController {
  HazardMonitorController();

  final instruments = <HazardInstrumentEntity>[].obs;
  final key = GlobalKey<CustomEasyRefreshListState>();

  HazardMonitorType type = HazardMonitorType.dcs;

  Future<List<HazardInstrumentEntity>> getInstruments(String hazardId) async {
    instruments.clear();
    final res = await HttpUtil().generateGetRequest<List<HazardInstrumentEntity>>(
      Interface.hazardForecast,
      queryParameters: {
        'targetType': type.toInt,
        'hazardCode': hazardId,
      },
    ).execute();
    res.onSuccess((result) {
      instruments.value = result.data ?? [];
      update();
    });
    return res.data ?? [];
  }

  void getData() {
    final selected = Get.find<MajorHazardController>().selectedHazard;
    final code = selected?.hazardCode;
    getInstruments(code ?? '');
  }

  @override
  void onReady() {
    super.onReady();
    getData();
  }
}
